package io.renren.modules.workmanagement.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.entity.ClassEntity;
import io.renren.modules.workmanagement.service.ClassService;
import io.renren.modules.workmanagement.vo.ClassVO;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author yewei
 * @email sunlightcs@gmail.com
 * @date 2022-02-19 16:02:49
 */
@RestController
@RequestMapping("workmanagement/class")
@RequiredArgsConstructor
public class ClassController extends AbstractController {

    private final ClassService classService;

    /**
     * 修改学生用户状态
     */
    @RequestMapping("/changeStuStatus")
    public R changeStuStatus(Long stuNo,Integer status) {
        classService.changeStuStatus(stuNo,status);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("department:sys:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put(UserConstans.USER_ID_KEY, userId);
        PageUtils page = classService.queryPage(params);

        return R.ok().put("page", page);
    }





    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("department:sys:info")
    public R info(@PathVariable("id") Integer id) {
        ClassVO classVO = classService.getClassById(Long.parseLong(id.toString()));

        return R.ok().put("classEntity", classVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("department:sys:save")
    public R save(@RequestBody ClassEntity classEntity) {
        Long userId = getUserId();
        classService.saveClass(classEntity, userId);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("department:sys:update")
    public R update(@RequestBody ClassEntity classEntity) {
        try {
            classService.updateById(classEntity);
        }catch (Exception e){
            throw new RRException("该系部下已经存在相同名称的班级，不能修改");
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("department:sys:delete")
    public R delete(@RequestBody Integer[] ids) {
        classService.removeByIdsExt(Arrays.asList(ids));

        return R.ok();
    }

}
